package com.ykdl.member.kid.regandlogin;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.base.QLog;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.ResetPasswordBean;
import com.ykdl.member.kid.circle.RecommendCircle;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.marketcard.CardPersonalInformationActivity;
import com.ykdl.member.kid.messagebox.MyMessageAcivity1;
import com.ykdl.member.kid.messagebox.OfficialMessageActivity1;
import com.ykdl.member.kid.util.ResoureUtil;
import com.ykdl.member.views.MyWheelView;
import com.ykdl.member.views.MyWheelView.OnWheelViewDataChanger;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;
/**
 * 完善资料
 * @author wangcaisheng
 *
 */
public class PerfectUserInfoActivity extends BaseScreen implements OnTouchListener{
	private EditText baby_birthday ,user_place;
	private MyWheelView wheel_time;
	private MyWheelView location;
	private long babybirthday;
	final String[] genderaray = new String[] {"女宝宝","男宝宝"};
	private int gender_index=-1;
	private ImageView mImgSex;
	private TextView gender;
	private boolean isSetTime = false;
	private Button back_bt;
	private boolean isShow = false;
	 private String place_string="" ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.perfectuserinfoactivity_xml);
		ResoureUtil.activityList.add(this);
		isShow = this.getIntent().getBooleanExtra("isShow", false);
		init();
	}
	public void init() {
		isSetTime = false;
		baby_birthday = (EditText)findViewById(R.id.baby_birthday);
		user_place = (EditText)findViewById(R.id.user_place);
		baby_birthday.setOnTouchListener(this);
		user_place.setOnTouchListener(this);
		gender = (TextView) findViewById(R.id.iask_gender);
		back_bt = (Button)findViewById(R.id.back_bt);
		if (isShow) {
			back_bt.setVisibility(View.VISIBLE);
		}else {
			back_bt.setVisibility(View.INVISIBLE);
		}
		findViewById(R.id.back_bt).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				go(RegAndLoginActivity.class);
			}
		});
		findViewById(R.id.turn_ok).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				profileUserInfo();
			}
		});
		findViewById(R.id.layerSex).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String tt = gender.getText().toString();
				AlertDialog alertDialog = new AlertDialog.Builder(
						PerfectUserInfoActivity.this).setSingleChoiceItems(genderaray,
							gender_index, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								gender.setText(genderaray[which]);
								if (which == 1) {
									mImgSex.setImageResource(R.drawable.icon_male);
								} else {
									mImgSex.setImageResource(R.drawable.icon_female);
								}
								gender_index = which;
								dialog.cancel();
							}
						}).create();
				alertDialog.show();
			}
		});
		mImgSex = (ImageView) findViewById(R.id.imgSex);
		wheel_time = (MyWheelView) findViewById(R.id.account_wheel_time);
		wheel_time.setWheelView(MyWheelView.CALENDAR);
		wheel_time.findViewById(R.id.my_wheelview_cancel).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				wheel_time.setMyGone();
			}
			
		});
		wheel_time.setOnWheelViewDataChanger(new OnWheelViewDataChanger() {

			@Override
			public void updataByChanger(String data) {
				// TODO Auto-generated method stub
				((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
				.hideSoftInputFromWindow(PerfectUserInfoActivity.this.getCurrentFocus()
						.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  
				String[] time = data.split(",");
				baby_birthday.setTextColor(Color.parseColor("#3e3e3e"));
				baby_birthday.setText(time[0]
						+ "年"
						+ time[1]
						+ "月"
						+ time[2]
						+ "日"
						);
				Date now = new Date();
				now.setYear(Integer.parseInt(time[0])-1900);
				now.setMonth(Integer.parseInt(time[1])-1);
				now.setDate(Integer.parseInt(time[2]));
				babybirthday =now.getTime()/1000;
				isSetTime = true;
			}
			
		});
		location = (MyWheelView) findViewById(R.id.select_location_layout);
		location.setWheelView(MyWheelView.CHINA);
		location.setOnWheelViewDataChanger(new OnWheelViewDataChanger() {
			@Override
			public void updataByChanger(String data) {
				QLog.debug("data", data);
				String[] datas = data.split("&#&");
				String[] contents = datas[0].split(",");
				String result = "";
				for (int i = 0; i < contents.length; i++) {
					if (!"".equals(result)) {
						result += ",";
					}
					result += contents[i];
				}
				user_place.setText(result);
			}
		});
	}
	
	
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.baby_birthday:
			((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
			.hideSoftInputFromWindow(PerfectUserInfoActivity.this.getCurrentFocus()
					.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  
			wheel_time.setMyVisible();
			break;
		case R.id.user_place:
			location.setMyVisible();
			break;

		default:
			break;
		}
		return false;
	}
	public void profileUserInfo() {
		String placeString = user_place.getText().toString() ;
		if (!isSetTime) {
			Toast.makeText(mContext, "您还没有填写宝宝生日，请填写", 1).show();
			return;
		}
		if (gender_index<0) {
			Toast.makeText(mContext, "您还没有填写宝宝性别，请填写", 1).show();
			return;
		}
		if (placeString.equals("")) {
			Toast.makeText(mContext, "您还没有填写地址，请填写", 1).show();
			return;
		}
		place_string = placeString;
		showProgress("正在提交请稍等...");
		String  pageurl = KidConfig.CHANGE_PROFILE;
	
		try {
			placeString = URLEncoder.encode(placeString, "utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpParameters bodyParams = new HttpParameters();
		bodyParams.put("gender", String.valueOf(gender_index));
		bodyParams.put("birthday", String.valueOf(babybirthday));
		bodyParams.put("address", placeString);
//		bodyParams.put("signature", placeString);
		
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl, bodyParams, null);
		 TaskManager.startHttpRequest(request, new profileUserInfoTag(),  ResetPasswordBean.class);
	}
	class profileUserInfoTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			if (t instanceof ResetPasswordBean) {
				finishProgress();
				ResetPasswordBean bean = (ResetPasswordBean)t;
				if (bean.getError()!=null) {
						Toast.makeText(mContext, "失败"+bean.getError(), 1).show();
				}else {
					if (bean.getStatus()==0) {
						Toast.makeText(mContext, "成功", 1).show();
						Intent intent = new Intent(PerfectUserInfoActivity.this,ChoooseCardActivity.class);
						intent.putExtra(KidAction.PLACE_STRING, place_string);
						PerfectUserInfoActivity.this.startActivity(intent);
					}else {
						Toast.makeText(mContext, "失败"+bean.getStatus(), 1).show();
					}
					
				}
			}
				
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
//		if (isShow) {
//			go(RegActivity.class);
//		}else {
			super.onBackPressed();
//		}
	}

}
